package apiUtils;

import android.text.TextUtils;

import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import apiUtils.params.Params;
import appUtils.LogUtil;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/5/3.
 */

public abstract class APISHelper {
    protected static Gson gson = new Gson();
    private static BaseAPIS baseAPIS = BaseAPIS.getInstance();

    protected abstract Params initParams() throws JSONException;

    protected void get(String tag, String url, CallBack callback) throws JSONException {
        baseAPIS.get(tag, url, initParams(), callback);
    }

    protected void post(String tag, String url, RequestBody body, CallBack callback) {
        baseAPIS.post(tag, url, body, callback);
    }

    protected void subscribe(Consumer<Integer> consumer) {
        Observable.just(1).observeOn(AndroidSchedulers.mainThread()).subscribe(consumer);
    }


    protected boolean isJsonArray(String str) {
        if (TextUtils.isEmpty(str))
            return false;
        try {
            new JSONArray(str);
            return true;
        } catch (JSONException e) {
            e.printStackTrace();
            LogUtil.log("isJsonArray JSONException = " + e.getMessage());
            return false;
        }
    }

    boolean isJson(String str) {
        if (TextUtils.isEmpty(str))
            return false;
        try {
            new JSONObject(str);
            return true;
        } catch (JSONException e) {
            e.printStackTrace();
            LogUtil.log("isJson JSONException = " + e.getMessage());
            return false;
        }
    }
}
